package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.web.dao.ISelectAreaDao;
import com.situ.web.entity.Area;
import com.situ.web.entity.City;
import com.situ.web.entity.Province;
import com.situ.web.util.JDBCUtil;

public class SelectAreaDaoImpl implements ISelectAreaDao {

	@Override
	public List<Province> selectProvince() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Province> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,province,sort from tm_province;";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				long id = resultSet.getLong("id");
				String province = resultSet.getString("province");
				int sort = resultSet.getInt("sort");
				Province province2 = new Province(id, province, sort);
				list.add(province2);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<City> selectCity(int province_id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<City> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,city,province_id from tm_city where province_id = ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, province_id);
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				long id = resultSet.getLong("id");
				String city = resultSet.getString("city");
				long provinceIds = resultSet.getLong("province_id");
				City city2 = new City(id, city, provinceIds);
				list.add(city2);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public List<Area> selectArea(int cityId) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ArrayList<Area> list = new ArrayList<>();
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,area,city_id from tm_area where city_id = ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, cityId);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				long id = resultSet.getLong("id");
				String area = resultSet.getString("area");
				int city_id = resultSet.getInt("city_id");
				Area area2 = new Area(id, area, city_id);
				list.add(area2);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

}
